/*
******************************************************************************************************* 
**  Copyright (C) 2019, 苏州检易生物科技有限公司 
**  All rights reserved. 
** 
**  FileName:       	spi_flash.c
**  Description:	
**  Author:        	 
**  Version				v0.1
**  Date:           	2019-06-21
**	Function List： 
**  History:         
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	调试开关
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                           		 头文件
*********************************************************************************************************
*/
#include "system_file.h"
/*
*********************************************************************************************************
*                                              	宏定义
*********************************************************************************************************
*/
using namespace std;
/*
*********************************************************************************************************
*                                              	结构体定义
*********************************************************************************************************
*/

 
/*
*********************************************************************************************************
*                                              	函数原型声明
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*                                              	全局变量定义
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                              	模块静态变量定义
*********************************************************************************************************
*/
/*FIL 结构要4k字节 不宜放在栈里*/
static FIL iap_file;
static FIL err_file;
/*
*********************************************************************************************************
*                                              	函数定义
*********************************************************************************************************
*/
/**
  * @brief 
  * @param : 
  * @retval ptr to iap file name
  */
char* iap_file_read(void)
{
    static char iap_file_buff[IAP_FILE_LEN];

    if(FR_OK != f_open(&iap_file,IAP_FILE_NAME,FA_READ))
    {
        return NULL;
    }
    uint32_t read_size;
    FRESULT rlt = f_read(&iap_file,iap_file_buff,sizeof(iap_file_buff),&read_size);
    if(rlt != FR_OK)
    {
        return NULL;
    }
    if(read_size == sizeof(iap_file_buff))
    {
        ErrorLog("IAP file reach max size;\r\n");
    }
    f_close(&iap_file);
    return iap_file_buff;
}
/**
  * @brief 
  * @param : 
  * @retval FILE_SUCCESS or FILE_FAILED
  */
uint8_t iap_file_write(char* buff)
{
    if(FR_OK != f_open(&iap_file,IAP_FILE_NAME,FA_CREATE_ALWAYS))
    {
        Error_Log("Iap file open failed!\r\n");
        return FILE_FAILED;
    }
    uint32_t write_size = strlen(buff);
    if(write_size >= IAP_FILE_LEN)
    {
        Critical_Log("Iap file name over size!\r\n");
        return FILE_FAILED;
    }
    if(FR_OK != f_write(&iap_file,buff,write_size,&write_size))
    {
        Error_Log("Iap file write failed!\r\n");
        return FILE_FAILED;
    }
    f_close(&iap_file);
    return FILE_SUCCESS;
}
uint8_t iap_file_clear(void)
{
    f_unlink(IAP_FILE_NAME);
    return FILE_SUCCESS;
}uint8_t error_file_wirte(char* buff)
{
    if(FR_OK != f_open(&err_file,ERROR_LOG_FILE_NAME,FA_OPEN_APPEND | FA_WRITE))
    {
		return FILE_FAILED;
    }
	if(f_size(&err_file) > ERROR_LOG_FILE_MAX)
	{
		f_close(&err_file);
		f_unlink(ERROR_LOG_FILE_NAME);
		if(FR_OK != f_open(&err_file,ERROR_LOG_FILE_NAME,FA_OPEN_APPEND | FA_WRITE))
		{
			return FILE_FAILED;
		}
	}
    uint32_t write_size = 0;
    if(FR_OK != f_write(&err_file,buff,strlen(buff),&write_size))
    {
        Error_Log("Error log file write failed!\r\n");
        return FILE_FAILED;
    }  
    f_close(&err_file);
    return FILE_SUCCESS;
}
   
    
uint8_t error_file_clear(void)
{
    f_unlink(ERROR_LOG_FILE_NAME);
    return FILE_SUCCESS;
}
CONFIG_FILE::CONFIG_FILE(void)
{
    this->config_file_read();
}
uint8_t CONFIG_FILE::config_file_read(void)
{
    memset(this->file_buff,0,sizeof(this->file_buff));
    if(FR_OK != f_open(&this->fhandle,CONFIG_FILE_NAME,FA_READ))
    {
        Error_Log("Config file open failed!\r\n");
        return FILE_FAILED;
    }
    uint32_t read_size;
    FRESULT rlt = f_read(&this->fhandle,this->file_buff,CONFIG_FILE_LEN,&read_size);
    if(rlt != FR_OK)
    {
        return FILE_FAILED;
    }
    if(read_size == CONFIG_FILE_LEN)
    {
        ErrorLog("Config file reach max size;\r\n");
    }
    f_close(&this->fhandle);
    return FILE_SUCCESS;
}
/**
  * @brief 
  * @param : find 
  * @retval key value string
  */
char* CONFIG_FILE::config_file_get_value(char* key)
{
    static char value[CONFIG_MAX_KEY];
    char format[64];
    memset(format,0,sizeof(format));
    memset(value,0,sizeof(value));

    char* head = strstr(this->file_buff,key);
    if(head == NULL)
    {
        Error_Log("Find config para %s failed!\r\n",key);
        return NULL;
    }
    string str_head = head;
    sprintf(format,"^%s {0,}= {0,}(.*?))\\r\\n",key);
    regex pattern(format);
    smatch result;
    if(regex_search(str_head,result,pattern))
    {
        strcpy(value,result[0].str().c_str());
    }
    else{
        Error_Log("Find config para %s failed!\r\n",key);
        return NULL;
    }
    return value;
}
int CONFIG_FILE::config_file_get_int(char* key)
{
    char* value = config_file_get_value(key);
    if(value != NULL)
        return stoi(value);
		return 0;
}
uint8_t CONFIG_FILE::config_file_clear(void)
{
    f_unlink(CONFIG_FILE_NAME);
    memset(file_buff,0,sizeof(file_buff));
    return FILE_SUCCESS;
}